<?php
defined('__EAPI_BASE') or define('__EAPI_BASE', dirname(__FILE__));

class EAPICache extends EAPIDatabase{
	
	public function __Construct(){
		parent::EAPIDatabase();
		defined('TBL_CACHE') or define('TBL_CACHE', '`eapi_cache`');
		date_default_timezone_set('Etc/GMT+0');
	}
	public function __Destruct(){}
	
	public function checkCache($host,$path,$params){
		
		$cacheArr = array("host" => $host, "path" => $path, "params" => $params);
		
		//if debugging is turned on, print the EAPICache details
		if($this->apicore['__APIDebug']=="cache"){
			
			print "<pre>".__CLASS__.":".__LINE__."<br/>";print_r($cacheArr);print "</pre>";
			
		}
			
		$query = sprintf("SELECT * FROM ".TBL_CACHE." WHERE `host` = '%s' AND `path` = '%s' AND `params` = '%s' AND `cachedUntil` >= '%s'",
						mysql_real_escape_string($cacheArr['host']),
						mysql_real_escape_string($cacheArr['path']),
						mysql_real_escape_string($cacheArr['params']),	
						date($this->apicore['__APITime']));
						
		//print "<pre>".date($this->apicore['__APITime'])."<br/>".$query."</pre>";
		
		$result = $this->query($query) or die(mysql_error());
		if(!$result){
			//return false;
		}
		if($this->numRows($result)>=1){
			//print "true";
			return true;
		}
		//print "false";
		return false;

	}
	
	public function setCache($host,$path,$params,$xml,$cachedUntil){
		try{
			$cacheArr = array("host" => $host, "path" => $path, "params" => $params, "xml" => $xml, "time" => $cachedUntil);
			
			//if debugging is turned on, print the EAPIUser details
			if($this->apicore['__APIDebug']=="cache"){
				
				print "<pre>".__CLASS__.":".__LINE__."<br/>";print_r($cacheArr);print "</pre>";
				
			}
			
			// CHECK IF IN CACHE ALREADY //
	
			// IF IN CACHE
			if($this->checkCache($cacheArr['host'], $cacheArr['path'], $cacheArr['params'])){
			// 		UPDATE CACHE INFO
			//print "CACHE UPDATE";
				$query = sprintf("UPDATE ".TBL_CACHE." SET `host` = '%s', `path` = '%s', `params` = '%s', `content` = '%s', `cachedUntil` = '%s' WHERE `path` = '%s' AND `params` = '%s' AND `cachedUntil` <= '%s'",
								mysql_real_escape_string($cacheArr['host']),
								mysql_real_escape_string($cacheArr['path']),
								mysql_real_escape_string($cacheArr['params']),
								mysql_real_escape_string($cacheArr['xml']),
								mysql_real_escape_string($cacheArr['time']),
								mysql_real_escape_string($cacheArr['path']),
								mysql_real_escape_string($cacheArr['params']),
								date($this->apicore['__APITime'])
								);
								
				//print "<pre>".date($this->apicore['__APITime'])."<br/>".$query."</pre>";
				$result = $this->query($query) or die(mysql_error());
				//print_r($this->query($query));
				
				if($result){
					
					return true;
				}
				
				throw new EAPIException('Unable To Update Cache',605.1);
			// ELSE
			}else{
			// 		INSERT INTO CACHE
			//print "CACHE INSERT";
				$query = sprintf("INSERT INTO ".TBL_CACHE." (`host`,`path`,`params`,`content`,`cachedUntil`) VALUES ('%s','%s','%s','%s','%s')",
								mysql_real_escape_string($cacheArr['host']),
								mysql_real_escape_string($cacheArr['path']),
								mysql_real_escape_string($cacheArr['params']),
								mysql_real_escape_string($cacheArr['xml']),
								mysql_real_escape_string($cacheArr['time']));
				$result = $this->query($query) or die(mysql_error());
				
				//print $query;
				if($result){
					return true;
				}
				throw new EAPIException('Unable To Create New Cache',605.2);
			// ENDIF
			}
		}catch (EAPIException $e){
			print $e;
			die();
		}
	}
	
	public function getCache($path,$params){
				
		$cacheArr = array("path" => $path, "params" => $params);
		
		//if debugging is turned on, print the EAPIUser details
			if($this->apicore['__APIDebug']=="cache"){
				
				print "<pre>".__CLASS__.":".__LINE__."<br/>";print_r($cacheArr);print "</pre>";
				
			}

		$query = sprintf("SELECT * FROM ".TBL_CACHE." WHERE `path` = '%s' AND `params` = '%s'",
						mysql_real_escape_string($cacheArr['path']),
						mysql_real_escape_string($cacheArr['params']));
		$result = $this->query($query) or die(mysql_error());
		$row = $this->fetchAssoc($result);
		//print_r($query);
		
		if(!empty($row)){
			return $row['content'];
		}
		
		return false;
			
	}
};
?>